package com.ivvi.autoreco;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ivvi.autoreco.widget.StickyGridHeadersSimpleAdapter;

import org.opencv.ivvi.custom.CropType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by duanxianping on 2017/4/11.
 */

public class DoneAdapter extends CommonAdapter implements StickyGridHeadersSimpleAdapter {

    private List<GridItem> items = new ArrayList<>();

    public DoneAdapter(Context context) {
        super(context);
    }

    public void setItems(List<GridItem> items) {
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    public void append(List<GridItem> items) {
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    private Object object = new Object();

    public void append(GridItem item) {
        synchronized (object) {
            this.items.add(0, item);
            notifyDataSetChanged();
        }
    }

    @Override
    public Object getHeaderId(int position) {
        if (items == null || items.size() == 0) {
            return 0;
        }
        return items.get(position).getHeadId();
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        final HeaderViewHolder headerViewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grid_2dto3d_header, parent, false);
            headerViewHolder = new HeaderViewHolder();
            headerViewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_grid_header);

            convertView.setTag(R.id.tv_grid_header, headerViewHolder);
        } else {
            headerViewHolder = (HeaderViewHolder) convertView.getTag(R.id.tv_grid_header);
        }
        bindHeaderData(headerViewHolder, position, convertView);
        return convertView;
    }

    private void bindHeaderData(HeaderViewHolder headerViewHolder, int position, View convertView) {
        GridItem item = (GridItem) getItem(position);
        if (item != null) {
            if (headerViewHolder != null) {
                headerViewHolder.tvTime.setText((String) item.getHeadId());
            }
        }
    }


    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {

        if (items.size() == 0) {
            return 0;
        }

        return items.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grid_2dto3d_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv3d = (ImageView) convertView.findViewById(R.id.iv_3d);
            viewHolder.tvSpendTime = (TextView) convertView.findViewById(R.id.tv_spendTime);
            viewHolder.tvLr = (TextView) convertView.findViewById(R.id.tv_lr);
            viewHolder.tvTb = (TextView) convertView.findViewById(R.id.tv_tb);
            viewHolder.tvType = (TextView) convertView.findViewById(R.id.tv_type);
            convertView.setTag(R.id.iv_3d, viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag(R.id.iv_3d);
        }
        bindData(viewHolder, position, convertView);
        return convertView;
    }

    private void bindData(ViewHolder viewHolder, int position, View convertView) {
        GridItem item = (GridItem) getItem(position);
        if (item != null) {
            Glide.with(context)
                    .load(item.getAbsPath())
//                    .bitmapTransform(new Half3d(context, CropType.LEFT), new CenterCrop(context))
                    .centerCrop()
                    .crossFade()
                    .into(viewHolder.iv3d);

            double[] d = item.getsDegree();

            viewHolder.tvSpendTime.setText("time:" + String.valueOf(item.getTime()) + "ms");
            viewHolder.tvLr.setText("lr:" + String.valueOf(d[0]));
            viewHolder.tvTb.setText("tb:" + String.valueOf(d[1]));

            if (item.getCropType() == CropType.LEFT) {
                viewHolder.tvType.setText("左右");
            } else if (item.getCropType() == CropType.TOP) {
                viewHolder.tvType.setText("上下");
            } else {
                viewHolder.tvType.setText("2D");
            }

        }

        convertView.setTag(item);
    }

    static class ViewHolder {
        public ImageView iv3d;
        private TextView tvSpendTime;
        private TextView tvLr;
        private TextView tvTb;
        private TextView tvType;
    }

    static class HeaderViewHolder {
        public TextView tvTime;
    }
}
